"use strict"

var Router = require('koa-router');
var config = require('../../../config/config.js');
var Shop = require('../../../model/shop.js');
var ShopDao = require('../../../dao/shop.js');

console.log('router:' + config.baseUrl + '/v1/shop');

var router = new Router({
    prefix: config.baseUrl + '/v1/shop'
});

router.get('/test', function*() {
    // console.log('%s %s', this.method, this.url);
    this.body = 'test work fine';
});


/**
 * 添加商铺接口
 {
   "name": "shop-test",
   "status": "online",
   "address": "天河区天河路208号粤海喜来登酒店8楼",
   "telephone": "020-66822580",

   "province_code": null,
   "province_name": "广东",
   "city_code": null,
   "city_name": "广州",
   "distinct_code": null,
   "distinct_name": "天河区",
   "loc": [23.13268,113.322],
   "regions": ["天河区", "天河城/体育中心"],

   "categories": ["美发", "美甲"],
   "ware_categories":[],
   "ware_ids":[],

   "decoration_grade": 0,
   "product_grade": 0,
   "service_grade": 0,
   "review_count":0
 }
 */
router.post('/', function*() {
    console.log('---raw body---');
    console.log(this.request.body);
    console.log('---raw body---');
    // this.body = this.request.body; //echo
    // return;
    try {
        var inParams = this.request.body;
        if (typeof inParams === 'string') {
            inParams = JSON.parse(this.request.body);
        }
    } catch (e) {
        console.log('error : input must be json');
        this.body = 'error : input must be json';
        return;
    }
    // console.log(inParams);
    // return;

    var aShop = new Shop(inParams);
    console.log(aShop);

    //
    try {
        var result = yield ShopDao.save(aShop);
        this.body = JSON.stringify(result);
    } catch (e) {
        this.body = 'save shop error ' + e.toString();
    }
});

// TODO:修改店铺接口
router.put('/:id', function*() {

});

/**
 * 为商店添加商品
 {
     "shopId":"xxx"
     "wareId": "xxx"
 }
 */
router.post('/addWare', function*() {
    var inParams = this.request.body;
    inParams = JSON.parse(inParams);
    var id = inParams.shopId;
    var wareInfo = {
        // category: inParams.category,
        wareId: inParams.wareId
    };

    var result = yield ShopDao.addWare(id, wareInfo);
    this.body = JSON.stringify(result);
});

/**
 * 根据条件查找 商铺,返回排序过的商铺
 {
    "query":{
        "distance":1000, // 
        "srcLoc":[12,34], // 距离计算的原点
        "category":"皮肤护理"
    },
    "order":{
        "by":"distance | price | grade",
        "direction":"desc | asc "
         // 距离/价格/好评, 从高/低->低/高
    },
    "pageIndex":3, // default 1
    "pageSize":10 // default 10
 }
 */
var DEFAULT_PAGE_SIZE = 10;
var DEFAULT_PAGE_INDEX = 1;

router.post('/getby', function*() {
    var inParams = JSON.parse(this.request.body);
    var query = inParams.query;
    var order = inParams.order;
    var pageIndex = inParams.pageIndex || DEFAULT_PAGE_INDEX;
    var pageSize = inParams.pageSize || DEFAULT_PAGE_SIZE;
    // check input args

    var result = yield ShopDao.getby(query, order, pageIndex, pageSize);
    this.body = result;
});

module.exports = router;